﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Planlama
{
    public partial class Kalite2Yeni : XForm
    {
        public Kalite2Yeni()
        {
            InitializeComponent();
        }

        private void Kalite2Yeni_Load(object sender, EventArgs e)
        {
            ((RepositoryItemGridLookUpEdit)gleCustomer.Edit).Assign(RI(Rp.V_Customer));
            deStart.EditValue = new DateTime(DateTime.Today.Year, 1, 1);
            deEnd.EditValue = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            //chkBitis.Checked = true;
            deEnd.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

            //chkBitis.CheckedChanged += chkBitis_CheckedChanged;
            splitContainerControl1.AutoSize();
            gridView1.OptionsView.EnableAppearanceOddRow = gridView1.OptionsView.EnableAppearanceEvenRow = false;

            ((RepositoryItemDateEdit)deStart.Edit).VistaCalendarViewStyle =
            ((RepositoryItemDateEdit)deEnd.Edit).VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;

            ((RepositoryItemDateEdit)deStart.Edit).EditMask =
            ((RepositoryItemDateEdit)deStart.Edit).DisplayFormat.FormatString =
            ((RepositoryItemDateEdit)deEnd.Edit).EditMask =
            ((RepositoryItemDateEdit)deEnd.Edit).DisplayFormat.FormatString = "MM/yyyy";

            chkDetay.CheckedChanged += chkDetay_CheckedChanged;
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            RefreshData();
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshData();
        }



        void chkDetay_CheckedChanged(object sender, EventArgs e)
        {
            RefreshData();
        }


        //void chkBitis_CheckedChanged(object sender, EventArgs e)
        //{
        //    deEnd.Visibility = chkBitis.Checked ? DevExpress.XtraBars.BarItemVisibility.Always :  DevExpress.XtraBars.BarItemVisibility.Never ;
        //    if (!chkBitis.Checked) deEnd.EditValue = null;
        //}

        DataSet d;
        //DataView dv;
        List<string> qtys = new List<string>();
        void RefreshData()
        {
            qtys = new List<string>();

            d = ExecuteDataSet("GetPLHata4", deStart.EditValue.ToDate().Month.ToInt(), deStart.EditValue.ToDate().Year.ToInt(), deEnd.EditValue.ToDate().Month.ToInt(), deEnd.EditValue.ToDate().Year.ToInt(), chkDetay.Checked, gleCustomer.EditValue.ToInt());
            gridView1.Bands.Clear();
            gridView1.Init(d.Tables[0]);
            gridView1.Images = bgvIcons;

            GridBand bandA = gridView1.Bands.AddBand("");
            GridBand bandK = gridView1.Bands.AddBand("Kumaş");
            GridBand bandU = gridView1.Bands.AddBand("Üretim");
            GridBand bandB = gridView1.Bands.AddBand("Baskı");
            GridBand bandL = gridView1.Bands.AddBand("Leke");
            GridBand bandN = gridView1.Bands.AddBand("Nakış");
            GridBand bandY = gridView1.Bands.AddBand("Yıkama");
            GridBand bandG = gridView1.Bands.AddBand("Diğer");

            //bandA.ImageIndex = 0;
            bandK.ImageIndex = 1;
            bandU.ImageIndex = 2;
            bandB.ImageIndex = 3;
            bandL.ImageIndex = 4;
            bandN.ImageIndex = 5;
            bandY.ImageIndex = 0;
            bandG.ImageIndex = 1;

            foreach (string colName in "AtelyeId,Kesilen,Yuklenen,ToplamHata,TopPct".Split(','))
                bandA.Columns.Add(gridView1.Columns[colName]);


            if (chkDetay.Checked)
            {
                foreach (string colName in "Dokuma Hatası,DPct,Kumaş Kırığı,KKPct,Renk Farkı,RPct".Split(','))
                    bandK.Columns.Add(gridView1.Columns[colName]);

                foreach (string colName in "Tela Hatası,TPct,Kesim Hatası,KHPct,İmalat Hatası,ImPct,İplik Çekmesi,ICPct,Makas Kesmiş,MPct,İlik Hatası,IPct,Ölçü Hatası,OPct,Dikiş Hatası,DHPct".Split(','))
                    bandU.Columns.Add(gridView1.Columns[colName]);
            }
            else
            {
                foreach (string colName in "Kumaş Hataları,KPct".Split(','))
                    bandK.Columns.Add(gridView1.Columns[colName]);

                foreach (string colName in "Üretim Hataları,UPct".Split(','))
                    bandU.Columns.Add(gridView1.Columns[colName]);
            }


            foreach (string colName in "Baskı Hatası,BPct".Split(','))
                bandB.Columns.Add(gridView1.Columns[colName]);

            foreach (string colName in "Leke,LPct".Split(','))
                bandL.Columns.Add(gridView1.Columns[colName]);

            foreach (string colName in "Nakış Hatası,NPct".Split(','))
                bandN.Columns.Add(gridView1.Columns[colName]);

            foreach (string colName in "Yıkama Hatası,YPct".Split(','))
                bandY.Columns.Add(gridView1.Columns[colName]);

            foreach (string colName in "Diğer,DgrPct".Split(','))
                bandG.Columns.Add(gridView1.Columns[colName]);

            bandA.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;


            gridView1.RowCellStyle -= gridView1_RowCellStyle;
            gridView1.RowCellStyle += gridView1_RowCellStyle;
            foreach (GridColumn col in gridView1.Columns)
            {
                if (col.FieldName == "AtelyeId")
                    continue;
                else if (col.FieldName.EndsWith("Pct"))
                {
                    col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    col.DisplayFormat.FormatString = "P2";
                    col.Caption = "%";
                }
                else
                    qtys.Add(col.FieldName);
            }

            foreach (string col in qtys)
            {
                gridView1.FormatAsNumber(0, col);
                gridView1.AddSummary(col);
                //DataRow[] r = AppSession.Lookups.Hata.Select("Name = '" + col + "'");
                //if (r.Length>0)
                //    if(r[0]["Grup"]!=DBNull.Value)
                //    gridView1.Columns[col].Caption = r[0]["Grup"] + "\r\n" + r[0]["Name"];
            }

          //  gridView1.MakeReadOnly();
            gridView1.BestFitColumns();

            gridView1.Click -= gridView1_Click;
            gridView1.Click += gridView1_Click;
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
                if (qtys.Contains(e.Column.FieldName) &&
                    e.Column.FieldName != "Kesilen" &&
                    e.Column.FieldName != "Yuklenen")
                    e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
                else if (e.Column.FieldName.EndsWith("Pct"))
                    e.Appearance.ForeColor = Color.SlateBlue;
        }

        void gridView1_Click(object sender, EventArgs e)
        {
            string col = gridView1.FocusedColumn.FieldName;
            if (gridView1.GetFocusedDataRow() != null)
            {
                int AtelyeId = gridView1.GetFocusedDataRow()["AtelyeId"].ToInt();
                DataView dv = d.Tables[1].AsDataView();
                if (col == "Diğer")
                    col = "";
                dv.RowFilter = "AtelyeId=" + AtelyeId + (col != "ToplamHata" ? " AND G='" + col + "'" : string.Empty);
                gridView2.Init(dv.ToTable("Detay"));
                gridView2.AddSummary("Adet");
                gridView2.MakeColumnInvisible("G");
                gridView2.MakeReadOnly();
                gridView2.BestFitColumns();
                splitContainerControl1.PanelVisibility = (gridView2.DataTable().Rows.Count == 0 ? DevExpress.XtraEditors.SplitPanelVisibility.Panel1 : DevExpress.XtraEditors.SplitPanelVisibility.Both);
            }
        }
    }
}
